using System;
using EPiServer.Common;
using EPiServer.Community.DocumentArchive;
using EPiServer.Community.DocumentArchive.Queries;

namespace EPiAbstractions.Community.DocumentArchive
{
    public class DocumentArchiveHandlerFacade : IDocumentArchiveHandlerFacade
    {
        private static DocumentArchiveHandlerFacade _instance;

        public DocumentArchiveHandlerFacade()
        {
            DocumentArchiveHandler.DocumentAdded += OnDocumentAdded;
            DocumentArchiveHandler.DocumentUpdated += OnDocumentUpdated;
            DocumentArchiveHandler.DocumentRemoved += OnDocumentRemoved;
            DocumentArchiveHandler.DocumentArchiveAdded += OnDocumentArchiveAdded;
            DocumentArchiveHandler.DocumentArchiveUpdated += OnDocumentArchiveUpdated;
            DocumentArchiveHandler.DocumentArchiveRemoved += OnDocumentArchiveRemoved;
        }

        public static DocumentArchiveHandlerFacade Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new DocumentArchiveHandlerFacade();

                return _instance;
            }

            set { _instance = value; }
        }

        #region IDocumentArchiveHandlerFacade Members

        public event EPiServerCommonEventHandler DocumentAdded;
        public event EPiServerCommonEventHandler DocumentUpdated;
        public event EPiServerCommonEventHandler DocumentRemoved;
        public event EPiServerCommonEventHandler DocumentArchiveAdded;
        public event EPiServerCommonEventHandler DocumentArchiveUpdated;
        public event EPiServerCommonEventHandler DocumentArchiveRemoved;

        public virtual Content GetContent(Int32 id)
        {
            return DocumentArchiveHandler.GetContent(id);
        }

        public virtual EPiServer.Community.DocumentArchive.DocumentArchive GetDocumentArchive(Int32 id)
        {
            return DocumentArchiveHandler.GetDocumentArchive(id);
        }

        public virtual EPiServer.Community.DocumentArchive.DocumentArchive AddDocumentArchive(
            EPiServer.Community.DocumentArchive.DocumentArchive archive)
        {
            return DocumentArchiveHandler.AddDocumentArchive(archive);
        }

        public virtual void UpdateDocumentArchive(EPiServer.Community.DocumentArchive.DocumentArchive archive)
        {
            DocumentArchiveHandler.UpdateDocumentArchive(archive);
        }

        public virtual void RemoveDocumentArchive(EPiServer.Community.DocumentArchive.DocumentArchive archive)
        {
            DocumentArchiveHandler.RemoveDocumentArchive(archive);
        }

        public virtual Document GetDocument(Int32 id)
        {
            return DocumentArchiveHandler.GetDocument(id);
        }

        public virtual Document AddDocument(Document document)
        {
            return DocumentArchiveHandler.AddDocument(document);
        }

        public virtual void UpdateDocument(Document document)
        {
            DocumentArchiveHandler.UpdateDocument(document);
        }

        public virtual void RemoveDocument(Document document)
        {
            DocumentArchiveHandler.RemoveDocument(document);
        }

        public virtual ContentCollection GetDocuments(EPiServer.Community.DocumentArchive.DocumentArchive archive, Int32 page,
                                                      Int32 pageSize, ContentSortOrder[] sortOrder)
        {
            return DocumentArchiveHandler.GetDocuments(archive, page, pageSize, sortOrder);
        }

        public virtual ContentCollection GetDocuments(EPiServer.Community.DocumentArchive.DocumentArchive archive, Int32 page,
                                                      Int32 pageSize, out Int32 totalItems, ContentSortOrder[] sortOrder)
        {
            return DocumentArchiveHandler.GetDocuments(archive, page, pageSize, out totalItems, sortOrder);
        }

        public virtual ContentCollection GetContents(EPiServer.Community.DocumentArchive.DocumentArchive archive, Int32 page, Int32 pageSize,
                                                     ContentSortOrder[] sortOrder)
        {
            return DocumentArchiveHandler.GetContents(archive, page, pageSize, sortOrder);
        }

        public virtual ContentCollection GetContents(EPiServer.Community.DocumentArchive.DocumentArchive archive, Int32 page, Int32 pageSize,
                                                     out Int32 totalItems, ContentSortOrder[] sortOrder)
        {
            return DocumentArchiveHandler.GetContents(archive, page, pageSize, out totalItems, sortOrder);
        }

        public virtual ContentCollection GetQueryResult(DocumentArchiveQuery query)
        {
            return DocumentArchiveHandler.GetQueryResult(query);
        }

        public virtual ContentCollection GetQueryResult(DocumentArchiveQuery query, Int32 page, Int32 pageSize)
        {
            return DocumentArchiveHandler.GetQueryResult(query, page, pageSize);
        }

        public virtual ContentCollection GetQueryResult(DocumentArchiveQuery query, Int32 page, Int32 pageSize, out Int32 totalItems)
        {
            return DocumentArchiveHandler.GetQueryResult(query, page, pageSize, out totalItems);
        }

        public virtual ContentCollection GetQueryResult(DocumentQuery query)
        {
            return DocumentArchiveHandler.GetQueryResult(query);
        }

        public virtual ContentCollection GetQueryResult(DocumentQuery query, Int32 page, Int32 pageSize)
        {
            return DocumentArchiveHandler.GetQueryResult(query, page, pageSize);
        }

        public virtual ContentCollection GetQueryResult(DocumentQuery query, Int32 page, Int32 pageSize, out Int32 totalItems)
        {
            return DocumentArchiveHandler.GetQueryResult(query, page, pageSize, out totalItems);
        }

        #endregion

        public virtual void OnDocumentAdded(String sender, EPiServerCommonEventArgs e)
        {
            if (DocumentAdded != null)
                DocumentAdded(sender, e);
        }

        public virtual void OnDocumentUpdated(String sender, EPiServerCommonEventArgs e)
        {
            if (DocumentUpdated != null)
                DocumentUpdated(sender, e);
        }

        public virtual void OnDocumentRemoved(String sender, EPiServerCommonEventArgs e)
        {
            if (DocumentRemoved != null)
                DocumentRemoved(sender, e);
        }

        public virtual void OnDocumentArchiveAdded(String sender, EPiServerCommonEventArgs e)
        {
            if (DocumentArchiveAdded != null)
                DocumentArchiveAdded(sender, e);
        }

        public virtual void OnDocumentArchiveUpdated(String sender, EPiServerCommonEventArgs e)
        {
            if (DocumentArchiveUpdated != null)
                DocumentArchiveUpdated(sender, e);
        }

        public virtual void OnDocumentArchiveRemoved(String sender, EPiServerCommonEventArgs e)
        {
            if (DocumentArchiveRemoved != null)
                DocumentArchiveRemoved(sender, e);
        }
    }
}